/**
 * 测试用删除接口测试用例
 *
 * Created by snowingsea on 2020/03/16.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/run-tests/self_designed');

module.exports = describe('/DELETE tests/:id.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion;
  let testId;
  let existsTestId;
  const newUserIds = [];
  function url(testId) {
    return `/tests/${testId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除测试用例
  it('it should delete test', function (done) {
    project.createRandomVersion()
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllTests(benchmark);
      })
      .then((res) => {
        testId = res[0].testId;
        existsTestId = res[1].testId;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(testId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);

            // 查询
            chai.request(uri)
              .get(url(testId))
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              });
          });
      })
      .catch(done);
  });

  // 不可删除不存在的测试用例
  it('it should fail to DELETE a test when the test is not exists', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .delete(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可删除他人的测试用例
  it('it should fail to delete a test when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((newTester) => {
        newUser = newTester;
        newUserIds.push(newTester.userId);
        return newTester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(testId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可向冻结项目版本删除文件
  it('it should fail to delete test in frozen project', function (done) {
    project.freezeVersion(projectVersion.versionId)
      .then(() => {
        chai.request(uri)
          .delete(url(existsTestId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(403);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可向冻结项目删除测试用例
  it('it should fail to delete test in frozen project', function (done) {
    project.creator.freezeProject(project.projectId)
      .then(() => {
        chai.request(uri)
          .delete(url(existsTestId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(403);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
